package cate.game.role.world.invite;

import cate.common.game.invite.InviteDef;
import cate.common.table.d.GDOperation;
import cate.common.table.invite.row.InviteLvUp30Row;
import cate.common.table.invite.row.InviteLvUp60Row;
import cate.game.GameBody;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcException;

import java.util.ArrayList;
import java.util.List;

public class InviteLvUpTask {
    @NoteField(value = "30级奖励领取tid")
    public List<Integer> received30;
    @NoteField(value = "60级奖励领取tid")
    public List<Integer> received60;
    private static final int LV_30 = 30;
    private static final int LV_60 = 60;

    public void init() {
        if (received30 == null) {
            received30 = new ArrayList<>();
        }
        if (received60 == null) {
            received60 = new ArrayList<>();
        }
    }

    private String getRewardCnf(GameBody game, int lv, int tid) {
        if(lv == LV_30) {
            InviteLvUp30Row row = game.table.invite.lvUp30.get(tid);
            if(row == null) {
                throw new EcException("找不到第"+tid+"项InviteLvUp30Row");
            }
            return row.rwdCnf;
        } else if(lv == LV_60) {
            InviteLvUp60Row row = game.table.invite.lvUp60.get(tid);
            if(row == null) {
                throw new EcException("找不到第"+tid+"项InviteLvUp60Row");
            }
            return row.rwdCnf;
        }
        throw new EcException("不支持的lv="+lv);
    }

    private long getNumCnf(GameBody game, int lv, int tid) {
        if(lv == LV_30) {
            InviteLvUp30Row row = game.table.invite.lvUp30.get(tid);
            if(row == null) {
                throw new EcException("找不到第"+tid+"项InviteLvUp30Row");
            }
            return tid;
        } else if(lv == LV_60) {
            InviteLvUp60Row row = game.table.invite.lvUp60.get(tid);
            if(row == null) {
                throw new EcException("找不到第"+tid+"项InviteLvUp60Row");
            }
            return tid;
        }
        throw new EcException("不支持的lv="+lv);
    }

    public void tryGetReward(Role role, int lv, int tid) {
        RoleInvite ri = role.getBase().invite;
        if(ri.inviteeList == null) {
            throw new EcException("ri.inviteeList == null").s();
        }
        long num = ri.inviteeList.stream().filter(a -> a.level >= lv).count();
        if (num < getNumCnf(role.getGame(), lv, tid)) {
            throw new EcException("未达到奖励领取条件").s();
        }
        List<Integer> recevied = lv == LV_30 ? received30 : received60;
        if(recevied.contains(tid)) {
            throw new EcException("已领取过啦").s();
        } else {
            recevied.add(tid);
        }
        new MixResAdder()
                .setRes(getRewardCnf(role.getGame(), lv, tid))
                .setOperation(GDOperation.INVITE)
                .setOperationSub(lv==LV_30? InviteDef.OPERATION_SUB_LV_UP_30: InviteDef.OPERATION_SUB_LV_UP_60)
                .setDialogReward(true)
                .exe(role);
    }
}
